<?php
namespace app\index\controller;
use app\index\model\Category;
use app\index\model\Collect;
use app\index\model\Question;
use app\index\model\Reply;
use app\index\model\User;
use app\index\model\Zan;
use think\Controller;
use think\Db;
use think\Loader;

class Jie extends Common
{
    public function index()
    {
        $qid = input('qid');

        (new Question)->where(['qid'=>$qid])->setInc('view_num');

        $uid = session('uid');

        $ques = Db::table('fly_question')
        ->alias('t1')
        ->field('t1.*,t2.username,t2.face,t2.level,t3.cname,t4.coid')
        ->join('fly_user t2','t1.uid = t2.uid')
        ->join('fly_category t3','t1.cid = t3.cid')
        ->join('fly_collect t4',"t4.qid=t1.qid and t4.uid='{$uid}'",'LEFT')
        ->where('t1.qid',$qid)
        ->find();

         $replys = Db::table('fly_reply')
        ->alias('t1')
        ->field('t1.*,t2.username,t2.face,t2.level,t4.zid')
        ->join('fly_user t2','t1.uid = t2.uid')
        ->join('fly_zan t4',"t4.rid=t1.rid and t4.uid='{$uid}'",'LEFT')
        ->where('t1.qid',$qid)
        ->order('t1.ctime desc')
        ->select();
        

        if($ques['ext']){
            $ques['ext'] = json_decode($ques['ext'],true);
        }

        $hotques = (new Question)->order('reply_num desc')->limit(10)->select();

        $cates = Category::all();

        $this->assign('cates',$cates);

        $this->assign('ques',$ques);
        $this->assign('replys',$replys);
        $this->assign('hotques',$hotques);
        $this->assign('title',$ques['title']);


        return $this->fetch();
    }
    public function add()
    {
    	$cates = Category::all();

    	$this->assign('cates',$cates); 

        return $this->fetch();

    }

    public function addpost()
    {
    	$data = input('post.');

    	// 加载验证器
        $validate = Loader::validate('jie');
        // 判断数据是否通过
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['uid'] = session('uid');

        $data['ctime'] = time();

        if($data['cid']==1){
            $data['ext'] = json_encode($data['ext']);
        }else{
            unset($data['ext']);
        }

        // 调用一个模型
        $question = new Question;

        $question->data($data);
        // 把数据插入数据 库
        $res = $question->save();

        // Db::table('fly_user')->where('uid', session('uid'))->setInc('kiss', $data['kiss']);

        (new User)->where(['uid'=>session('uid')])->setDec('kiss',$data['kiss']);

        if($res){
            $this->success("发布成功",url('index/jie/index',['qid'=>$question->qid]));
        }else{
            $this->error("发布失败");
        }

    }

    public function upload()
    {
        $file = request()->file('file');
    
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){

                $img = "/uploads/".date('Ymd').'/'.$info->getFilename();

                $this->error('上传成功','',['src'=>$img]);
                
            }else{
                $this->success($file->getError());
                // 上传失败获取错误信息
            }
        }
    }

    public function reply()
    {
        $data = input('post.');
        // 加载验证器
        $validate = Loader::validate('reply');
        // 判断数据是否通过
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $data['uid'] = session('uid');
        $data['ctime'] = time();

        $reply = new Reply($data);

        $res =  $reply->save();

        (new Question)->where(['qid'=>$data['qid']])->setInc('reply_num');

        if($res){
            $this->success("发布成功");
        }else{
            $this->error("发布失败");
        }
    }

    public function collect()
    {
        // 接收藏qid
        $qid = input('qid');
        // 查询当前 登录 的用户是否有收藏该问题
        $co = Collect::get(['uid'=>session('uid'),'qid'=>$qid]);
        // 如果有
        if($co){
            // 取消收藏 
            $res = Collect::destroy(['uid'=>session('uid'),'qid'=>$qid]);

            if($res){
                $this->success("取消收藏");
            }else{
                $this->error("发布失败");
            }

        }else{
            // 没有就增加收藏
            $data['uid'] = session('uid');
            $data['ctime'] = time();
            $data['qid'] = $qid;
            $co = new Collect($data);
            $res = $co->save();

            if($res){
                $this->success("收藏成功");
            }else{
                $this->error("发布失败");
            }
        }
    }

    public function zan()
    {
        // 接收藏qid
        $rid = input('rid');
        // 查询当前 登录 的用户是否有收藏该问题
        $co = Zan::get(['uid'=>session('uid'),'rid'=>$rid]);
        // 如果有
        if($co){
            $this->error("你已经点赞过");
        }else{
            // 没有就增加收藏
            $data['uid'] = session('uid');
            $data['ctime'] = time();
            $data['rid'] = $rid;
            $zan = new Zan($data);
            $res = $zan->save();

            (new Reply)->where(['rid'=>$rid])->setInc('zan_num');

            if($res){
                $this->success("点赞成功");
            }else{
                $this->error("点赞失败");
            }
        }
    }
}